var sessionList=null;

function updateShowSessionList(){
	if(listDivKind=="session"&&sessionList!=null){
		showSessionList(null);
	}
}

setInterval(updateShowSessionList, 1000);

function intoSessionList() {
	if (nowProjectIndex >= 0) {
		getSessionList(nowProjectIndex);
		listDivKind = "session";
		openListDiv();
	}
}

function cantIntoSessionList(){
	rat.pushInfo("这个项目没有开启会话功能！");
}

function getSessionList() {
	send("GET", "/@ServerSession?" + nowProjectIndex, true, "", showSessionList, null);
}

function closeSession(sessionId) {
	if (confirm("是否断开会话\n" + "\"" + sessionId + "\"")) {
		rat.qure();
		var json = {};
		json.projectIndex = nowProjectIndex;
		json.order = "Close";
		json.sessionId = sessionId;
		send("POST", "/@ServerSession", true, JSON.stringify(json), showSessionList, null);
	}
}

function showSessionList(text) {
	if(text != null && text != undefined){
		sessionList = JSON.parse(text);
	}
	var html = "";
	for (let i = 0; i < sessionList.length; i++) {
		let session = sessionList[i];
		let sessionId = session.sessionId;
		let maxLiveTime = JSON.parse(session.maxLiveTime);
		let createTime = JSON.parse(session.createTime);
		let lastVisitedTime = JSON.parse(session.lastVisitedTime);
		let nowTime = Date.now();
		let restTime = lastVisitedTime + maxLiveTime - nowTime;
		if(restTime <= 0){
			continue;
		}
		let useTime = nowTime - createTime;
		html += "<lable>会话</lable>";
		html += "<input type=\"button\" name=\"sessionId\" class=\"sessionId\" value=\"";
		html += sessionId;
		html += "\"/>";
		html += "<lable>使用</lable>";
		html += "<input type=\"button\" name=\"useTime\" class=\"time\" value=\"";
		html += useTime / 1000 +"秒";
		html += "\"/>";
		html += "<lable>剩余</lable>";
		html += "<input type=\"button\" name=\"restTime\" class=\"time\" value=\"";
		html += restTime / 1000 +"秒";
		html += "\"/>";
		html += "<button type=\"button\" class=\"close\" onclick=\"closeSession('" + sessionId +
			"')\">断开</button>";
		html += "<br>";
	}
	html += "<br><button type=\"button\" class=\"show\" onclick=\"getSessionList()\">刷新</button>";
	document.getElementById("ListTitle").innerHTML = "会话列表";
	document.getElementById("ListContent").innerHTML = html;
}